package com.yiren.backstage.player.announcer;

import android.media.MediaPlayer;

import java.io.FileInputStream;


/**
 * Time:2021/12/29 11:04
 * Author:xiaozan
 * Description:播报器抽象类
 */
public abstract class AbstractMediaPlayer {

    public MediaPlayer mediaPlayer;

    private boolean isPause = false;

    public AbstractMediaPlayer() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mediaPlayer.reset();
                return false;
            }
        });
    }

    /**
     * 播放音频
     *
     * @param filePath
     * @param onCompletionListenter
     */
    public void playSound(String filePath, MediaPlayer.OnCompletionListener onCompletionListenter) {

        if (mediaPlayer == null) {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    mediaPlayer.reset();
                    return false;
                }
            });
        } else {
            mediaPlayer.reset();
        }
        try {
            //mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnCompletionListener(onCompletionListenter);
            FileInputStream fileInputStream = new FileInputStream(filePath);
            mediaPlayer.setDataSource(fileInputStream.getFD());
            mediaPlayer.prepare();
            mediaPlayer.start();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }


    /**
     * 暂停
     */
    public synchronized void pause() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            isPause = true;
        }
    }

    //停止
    public synchronized void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            isPause = false;
        }
    }

    /**
     * resume继续
     */
    public synchronized void resume() {
        if (mediaPlayer != null && isPause) {
            mediaPlayer.start();
            isPause = false;
        }
    }

    public boolean isPause() {
        return isPause;
    }

    public void setPause(boolean isPause) {
        this.isPause = isPause;
    }

    /**
     * release释放资源
     */
    public void release() {
        if (mediaPlayer != null) {
            isPause = false;
            mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    public synchronized void reset() {
        if (mediaPlayer != null) {
            mediaPlayer.reset();
            isPause = false;
        }
    }

    /**
     * 判断是否在播放
     *
     * @return
     */
    public synchronized boolean isPlaying() {
        return mediaPlayer != null && mediaPlayer.isPlaying();
    }

}
